#!/usr/bin/perl
#mdmjobs-lib.pl

do "../web-lib.pl";
&init_config();
%access = &get_module_acl();
&foreign_require("mdmsearch", "mdmsearch-lib.pl");
&foreign_require("mdmgroups", "mdmgroups-lib.pl");
&foreign_require("mdmhistory","mdmhistory-lib.pl");

#select_members
#if $_[1] is "nothis",then [This server] will not be included
#if $_[2] is not "" ,then the memberlist is not empty.
sub select_members()
{
	local @devlist;
	@devlist=&foreign_call("mdmsearch","list_controlledservs");
	local $grpaddr=$_[0];
	local @grplist= sort {$a cmp $b } @$grpaddr;
	
	print "<table class=mode1 border=0 cellspacing=0 cellpadding=0>\n";
	print "<tr><table class=mode1 border=0 cellspacing=0 cellpadding=0>\n";
	print "<tr><hr width=550>";
	print "<tr><h4>$text{'index_selgroup'}</h4></tr>\n";
	print "<td><table class=mode1 border=0 cellspacing=0 cellpadding=0>\n";
	print "<tr><b>$text{'info_members'}</b></tr>";
	print "<tr><select class=list name=memberlist size=15 multiple>";
	if ($_[2] ne ""){
		local @locallist=split(/,/,$_[2]);
		foreach $d (@locallist){
			print "<option value=\"$d\">$d\n";
		}
	}
	print "</select></tr>";
	print "<tr>$text{'info_grpform'}</tr>";
	print "<input name=listinput type=hidden value = ''>";
	print "</table></td>";
	print "<td><table class=mode1 border=0 cellspacing=0 cellpadding=>\n";
	print "<tr><td><table class=mode1 border=0 cellspacing=0 cellpadding=10>\n";
	print "<td><table class=mode1 border=0 cellspacing=0 cellpadding=5>\n";
	print "<tr><td><input class=sbutton type=button onclick='addselect1();' value=\'$text{'btadd'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='removeselect1();' value=\'$text{'btremove'}\'></td></tr>\n";
	print "</table></td>";
	print "<td><table class=mode1 border=0 cellspacing=0 cellpadding=0>\n";
	print "<tr><td>$text{'info_devlist'}</td></tr>";
	print "<tr><td><select class=list name=devicelist size=8 multiple>";
	if ($_[1] ne "nothis"){
		print "<option value=\"[This server]\">[This server]\n";
	}
	foreach(@devlist){
		print "<option value=\"[$_->{'id'}]$_->{'ip'}\">[$_->{'id'}]$_->{'ip'}\n";
	}
	print "</select></td></tr>";
	print "</table></td>";
	print "</table></td></tr>";
	print "<tr><td><table class=mode1 border=0 cellspacing=0 cellpadding=10>\n";
	print "<td><table class=mode1 border=0 cellspacing=0 cellpadding=5>\n";
	print "<tr><td><input class=sbutton type=button onclick='addselect2();' value=\'$text{'btadd'}\'></td></tr>\n";
	print "<tr><td><input class=sbutton type=button onclick='removeselect2();' value=\'$text{'btremove'}\'></td></tr>\n";
	print "</table></td>\n";
	print "<td><table class=mode1 border=0 cellspacing=0 cellpadding=0>\n";
	print "<tr><td>$text{'info_grplist'}</td></tr>\n";
	print "<tr><td><select class=list name=grouplist size=8 multiple>\n";
	foreach(@grplist){
		local $grpname='@'.$_;
		print "<option value=\"$grpname\">$grpname\n";
	}
	print "</select></td></tr>\n";
	print "<p><tr><td>$text{'click_grpinfo1'}<a href='../mdmgroups/index.cgi'>$text{'grpinfo'}</a>$text{'click_grpinfo2'}</td></tr>\n";
	print "</table></td>\n";
	print "</table></td></tr>\n";
	print "</table></tr>\n";
	print "</table></tr>\n";
	
	
print <<EOF;
<script>
function find_item_in_grouplist(text1)
{
	var rt = 0;
	var num;
	for(num = 0; num < document.forms[0].memberlist.options.length; num++)
	{
		if(document.forms[0].memberlist.options[num].text == text1)
		{
			rt = 1;
		}
	}
	return rt;
}

function addselect1()
{
	var j= document.forms[0].memberlist.options.length;

	for(i = document.forms[0].devicelist.options.length; i > 0; i--)
	{
		if(document.forms[0].devicelist.options[i - 1].selected)
		{

			if(find_item_in_grouplist(document.forms[0].devicelist.options[i - 1].text) == 0)
			{
				document.forms[0].memberlist.options[j] = new Option(document.forms[0].devicelist.options[i - 1].text, document.forms[0].devicelist.options[i - 1].text);
				j = document.forms[0].memberlist.options.length;
			}
		}
	}

	if(document.forms[0].memberlist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].memberlist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].memberlist.options.length; i++)
	{
		document.forms[0].listinput.value += "," + document.forms[0].memberlist.options[i].text;
	}
}

function removeselect1()
{
	for(i = document.forms[0].memberlist.options.length; i > 0; i--)
	{
		if(document.forms[0].memberlist.options[i - 1].selected)
		{
			document.forms[0].memberlist.options[i - 1] = null;
		}
	}

	if(document.forms[0].memberlist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].memberlist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].memberlist.options.length; i++)
	{
		document.forms[0].listinput.value += "," + document.forms[0].memberlist.options[i].text;
	}
	//history.go(0);
}

function addselect2()
{
	var j= document.forms[0].memberlist.options.length;

	for(i = document.forms[0].grouplist.options.length; i > 0; i--)
	{
		if(document.forms[0].grouplist.options[i - 1].selected)
		{

			if(find_item_in_grouplist(document.forms[0].grouplist.options[i - 1].text) == 0)
			{
				document.forms[0].memberlist.options[j] = new Option(document.forms[0].grouplist.options[i - 1].text, document.forms[0].grouplist.options[i - 1].text);
				j = document.forms[0].memberlist.options.length;
			}
		}
	}

	if(document.forms[0].memberlist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].memberlist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].memberlist.options.length; i++)
	{
		document.forms[0].listinput.value += "," + document.forms[0].memberlist.options[i].text;
	}
}

function removeselect2()
{
	for(i = document.forms[0].memberlist.options.length; i > 0; i--)
	{
		if(document.forms[0].memberlist.options[i - 1].selected)
		{
			document.forms[0].memberlist.options[i - 1] = null;
		}
	}

	if(document.forms[0].memberlist.options.length > 0)
	{
		document.forms[0].listinput.value = document.forms[0].memberlist.options[0].text;
	}
	else
	{
		document.forms[0].listinput.value = "";
	}

	for(i = 1; i < document.forms[0].memberlist.options.length; i++)
	{
		document.forms[0].listinput.value += "," + document.forms[0].memberlist.options[i].text;
	}
	//history.go(0);
}

</script>
EOF

}

sub inst_error
{
	local $inst_error_msg = join("", @_);
}


sub get_alertinfo
{
	opendir(DIR,"$config{'alertdir'}")||die "opendir failed:$!\n";
	my @list = grep(/\.mesg/,readdir(DIR));
	@list = sort {$a cmp $b} @list;
	closedir(DIR);
	
	my %listdev;
	if (@list){
		$listdev{'list'}->{'have'}='yes';
	}

	foreach(@list){
		my $dev=$_;
		$dev =~ s/.mesg.*//;
		$listdev{"$dev"}->{'status'}=1;
		if (/mesg0/){
			$listdev{"$dev"}->{'mesg0'}=1;
		}
		if (/mesg1/){
			$listdev{"$dev"}->{'mesg1'}=1;
		}
	}
	return (\%listdev);
}

#rm_alertfile
#remove the mesg file from the mdmjob/alert dir and the mdmjob dir
sub rm_alertfile
{
	my $file1=$config{'alertdir'}."*.mesg*";
	my $file2=$config{'linkdir'}."*.mesg*";

	system("rm -f $file1");
	system("rm -f $file2");
}

1;